package org.gunncs.core;

import com.sun.spot.peripheral.radio.RadioFactory;
import com.sun.spot.util.IEEEAddress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.gunncs.host.Command;

/**
 * A command line interface for CreatePlatform.
 * @author ajc
 */
public class RobotShell {

    public static final String RADIO_ADDRESS;
    
    private static final String UNKNOWN_COMMAND = "Unknown command";

    private String greetings;// = "Welcome to RSH";
    private String prompt = "#rsh $";// TIME_FORMAT +
    private String user;
    private static HashMap<String, RobotShell> sessions;

    static {
        sessions = new HashMap<String, RobotShell>();
        long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
        RADIO_ADDRESS = IEEEAddress.toDottedHex(ourAddr);
    }

//    public RobotShell(){
//        long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
////        GREETINGS= "Welcome, " + USER +" to rsh at " + IEEEAddress.toDottedHex(ourAddr);
//    }
    public static void main(String[] args) {
        RobotShell rs = new RobotShell();
        boolean loggedin = false;
        while (!loggedin) {
            loggedin = rs.login();
        }
        rs.execCommandLoop();
    }

//    private void init(){
//
//    }
    
    /**
     * Login to rsh. If a session of your username exists, then your login
     * fails.
     * @return true if logged in successfully
     */
    private boolean login() {
        try {
            System.out.println("CreatePlatform login:");
            System.out.println("Please press enter, provide username");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            user = br.readLine();
            System.out.println("RECEIVED:" + user);
            boolean collision = sessions.entrySet().contains(user);
            if (!collision) {
                sessions.put(user, this);
                greetings = "Welcome, " + user + " to CreatePlatform at " + RADIO_ADDRESS;
                prompt = "["+ user+"@"+"CreatePlatform ~]$";
            }
            return !collision;

        } catch (IOException ex) {
            Logger.getLogger(RobotShell.class.getName()).log(Level.SEVERE, null, ex);
            user = null;
            return false;
        }

    }

    private void execCommandLoop() {
        System.out.println(greetings);
        System.out.println();
        while (true) {
            try {
                //prompt for user input
                System.out.println(prompt);
                //get command and arguments (a line)
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String commandLine = br.readLine();

                //scan it
                Scanner scanner = new Scanner(commandLine);
                if (scanner.hasNext()) {
                    //We get the command that we want
                    final Command cmd = Command.get(scanner.next().toLowerCase());

                    //store the arguments in a list
                    ArrayList<String> params = new ArrayList<String>();
                    while (scanner.hasNext()) {
                        params.add(scanner.next());
                    }

                    //execute command with arguements
                    cmd.exec(params.toArray(new String[0]));

                }
                scanner.close();
//                br.close();
            } catch (IOException ex) {
                Logger.getLogger(RobotShell.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException e) {
                System.out.println(UNKNOWN_COMMAND);
            } catch (NullPointerException e) {
                System.out.println("ERROR: Null");
            }
        }
    }
}
